﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BankOCR
{
    public class Tokenizer: ITokenizer
    {
        public IEnumerable<DigitalSymbol> TokenizeLineOfSymbols(string topLine, string middleLine, string bottomLine)
        {
            VerifyLineLenghts(topLine, middleLine, bottomLine);

            var currentIndex = 0;
            while (true) {
                if (currentIndex + 2 >= topLine.Length) yield break;
                var topPart = topLine.Substring(currentIndex, 3);
                var middlePart = middleLine.Substring(currentIndex, 3);
                var bottomPart = bottomLine.Substring(currentIndex, 3);
                currentIndex += 3;

                yield return new DigitalSymbol(new[,] { 
                    { topPart[0], topPart[1], topPart[2] },
                    { middlePart[0], middlePart[1], middlePart[2] },
                    { bottomPart[0], bottomPart[1], bottomPart[2] } });
            }
        }

        private void VerifyLineLenghts(string topLine, string middleLine, string bottomLine)
        {
            if (topLine.Length != middleLine.Length || middleLine.Length != bottomLine.Length)
                throw new InvalidOperationException("All three lines must be the same length.");

            if (topLine.Length % 3 != 0 || middleLine.Length % 3 != 0 || bottomLine.Length % 3 != 0)
                throw new InvalidOperationException("All lines' lengths must be multiples of 3.");
        }
    }
}
